package com.qf.servlet;



import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.JsonUtils;
import com.qf.utils.ResultVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet(name = "userServlet",urlPatterns = "/userServlet")
public class UserServlet extends BaseServlet {
//    实例化service
    private UserService userService = new UserServiceImpl();
//    添加数据
    public void addUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//        获取前端传递的数据
        PrintWriter pw = response.getWriter();
        String uname = request.getParameter("uname");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
//        调用service添加数据的方法
        int i = userService.addUser(uname, password, sex,address, phone);
//        判断是否添加成功
        if (i>0){
            pw.print(1);
        }else {
            pw.print(0);
        }
    }
    public void loginUser(HttpServletRequest request,HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();

//        获取前端传递的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = userService.loginUser(username, password);
        if (user!=null){
            pw.print(true);
            request.getSession().setAttribute("user",user);
        }else {
            pw.print(false);
        }
    }



    public void  selectUserById(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String uid = request.getParameter("uid");
        User user = userService.selectUserById(uid == null ? 0 : Integer.parseInt(uid));
        JsonUtils.toJsonString(user,response);
    }

    public void deleteUid(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String uid = request.getParameter("uid");
        int num = userService.deleteUid(Integer.parseInt(uid));
        ResultVo<Integer> resultVo = new ResultVo<>();
        resultVo.setData(num);
        resultVo.setStatus(num > 0 ? 200:400);
        resultVo.setMessage(num > 0 ? "成功":"失败");
        JsonUtils.toJsonString(resultVo,response);

    }

    public void updateUser(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String uid = request.getParameter("uid");
        String uname = request.getParameter("uname");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");
        User user = new User(uid,uname,password,sex,address,phone);
        int num = userService.updateUser(user);
        ResultVo<Integer> resultVo = new ResultVo<>();
        resultVo.setData(num);
        resultVo.setStatus(num > 0 ? 200:400);
        resultVo.setMessage(num > 0 ? "成功":"失败");
        JsonUtils.toJsonString(resultVo,response);
    }
}
